package com.vietnam.power.dao.impl;

import org.hibernate.Cache;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vietnam.power.dao.CacheDao;

@Repository
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class CacheDaoImpl extends GenericDaoImpl implements CacheDao {

	public void clearAllCache() {
		Session session = getSessionFactory().getCurrentSession();

		if (session != null) {
			session.clear(); // 1st cache clear
		}

		Cache cache = getSessionFactory().getCache();
		// 2nd cache clear
		if (cache != null) {
			cache.evictCollectionRegions();
			cache.evictDefaultQueryRegion();
			cache.evictEntityRegions();
			cache.evictNaturalIdRegions();
			cache.evictQueryRegions();
		}
	}
}
